<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/plugins/chosen/chosen.jquery.js"></script>
    <link rel="stylesheet" href="/js/plugins/chosen/component-chosen.css"/>
    <script src="/js/common.js"></script>

    <script>
        $(function () {
            //获取当前登录用户信息
            var user = JSON.parse(sessionStorage.getItem("user"));
            //点击更换背景图片触发图片选择框
            $("#changeBtn").click(function () {
                $("#imgInput").click();
            })
            //文件更改事件
            $("#imgInput").change(function () {
                //有值提交表单
                if ($(this).val()) {
                    $("#imgForm").ajaxSubmit(function (data) {
                        if (data.status == 1) {
                            $("#coverUrl").attr("src", data.url);
                        }
                    })
                }
            })
            //获取所有的地区
            $.get('/regions', function (data) {
                var html = "";
                $.each(data, function (index, ele) {
                    html += "<option value='" + ele.id + "'>" + ele.name + "</option>";
                })
                $("#place").html(html);
                //初始化chosen组件
                $("#place").chosen({
                    disable_search_threshold: 10,//大于10条就显示搜索框
                    no_results_text: "无相关地区"//搜索时找不到的提示
                })
            })
            //设置url,表示表单提交地址,默认为新增地址
            var url = "/users/" + user.id + "/travels";
            //获取url上的id参数值,如果有值就是编辑,利用getParams方法
            var params = getParams();
            if (params.id) {
                //编辑表单提交地址
                url = "/travels/" + params.id;
                //编辑时需要修改表单的提交方式
                $("[name=_method]").val('put');
                //发请求(根据id获取游记对象)
                $.get("/travels/" + params.id, function (data) {
                    //渲染数据
                    $(".travels").renderValues(data);
                    //回显地区下拉框
                    //普通方式
                    $("#place option[value=" + data.place.id + "]").prop('selected', true);
                    //让插件执行更新操作
                    $("#place").trigger("chosen:updated");
                })
            }
            //默认草稿状态
            var state = 0;
            //针对表单的属性配置
            var options = {
                //表单提交地址
                url: url,
                beforeSubmit: function (arr) {
                    //在提交表单之前添加额外的参数,arr是需要提交的参数数组,用push
                    arr.push({name: "coverUrl", value: $("#coverUrl").attr("src")});//封面
                    arr.push({name: "title", value: $("#title").val()});//标题
                    //获取游记内容(使用编辑器的方法获取内容)
                    arr.push({name: "travelContent.content", value: elEditor.getContent()});
                    arr.push({name: "state", value: state});//状态
                },
                success: function (data) {
                    if (data.success) {
                        //跳转到我的游记页面
                        window.location.href = "/mine/travels.html";
                    } else {
                        alert(data.msg);
                    }
                }
            }
            //保存按钮
            $("#save").click(function () {
                $("#detailForm").submit();
            })
            // 发布按钮
            $("#release").click(function () {
                //待发布状态
                state = 1;
                $("#detailForm").submit();
            })
            //把表单变成异步请求,不会发生提交
            $("#detailForm").ajaxForm(options);


        })
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>
        <input type="file" name="pic" style="display: none" accept="image/gif,image/jpeg,image/jpg,image/png"/>
        <button type="button" class="btn" id="changeBtn">
            <i class="fa fa-pencil-square-o"></i>
            <small>更换封面</small>
        </button>
        <form action="/images" id="imgForm" method="post">
            <input type="file" name="file" style="display: none" id="imgInput">
        </form>
    </div>
    <div class="title">
        <input class="form-control" id="title" placeholder="请输入标题" render-value="title"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="travelContent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post">
            <input type="hidden" name="_method">
            <input type="hidden" name="id" render-value="id"/>
            <label>出行时间:</label>
            <input type="text" name="travelTime" class="form-control" onclick="WdatePicker()" render-value="travelTime">
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <select class="form-control" id="place" name="place.id">
            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" name="isPublic" render-attr="checked=isPublic"
                    >是否公开
                </label>
            </div>

        </form>
    </div>

    <hr/>

</div>
<!--Eleditor核心组件-->
<script src="/js/plugins/eleditor/Eleditor.min.js"></script>
<!--图片上传-->
<script src="/js/plugins/eleditor/webuploader.min.js"></script>
<script>
    var elEditor = new Eleditor({
        el: '#contentEditor',
        upload: {
            //图片上传地址
            server: '/images'
        }
    });
</script>
</body>

</html>